package com.cb.serviceimpl;

import com.cb.dao.LeaderboardDao;
import com.cb.pojo.Anticipation;
import com.cb.pojo.Film;
import com.cb.service.LeaderboardService;
import com.alibaba.fastjson.JSON;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;

@Service
public class LeaderboardServiceImpl implements LeaderboardService {


    Logger logger = Logger.getLogger("com.cb.serviceimpl.LeaderboardServiceImpl");

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    LeaderboardDao leaderboardDao;

    private String allOrderListRedis = "allOrderListRedis";

    @Override
    public String anticipation(HashMap json, HttpServletRequest request) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String now = df.format(new Date());
        String ypid = String.valueOf(json.get("ypid"));
        String ypname = String.valueOf(json.get("ypname"));
//        Film film = new Film();
//        film.setId(ypid)
//                .setYpname(ypname);
        Anticipation anticipation = new Anticipation();
        anticipation.setYpid(ypid)
                .setYpname(ypname)
                .setTime(now);
        int num;
        if (redisTemplate.opsForZSet().size(now)==0){
            num = 1;
            anticipation.setNumber(num);
            int flag = leaderboardDao.insertAnticipation(anticipation);
            redisTemplate.opsForZSet().add(now,ypname,1);
        }
        else{
            if (redisTemplate.opsForZSet().score(now,ypname)==null){
                num = 1;
                anticipation.setNumber(num);
                int flag = leaderboardDao.insertAnticipation(anticipation);
                redisTemplate.opsForZSet().add(now,ypname,1);
            }
            else{
                double score = redisTemplate.opsForZSet().score(now,ypname);
                num = (int) score+1;
                anticipation.setNumber(num);
                int flag = leaderboardDao.updateAnticipation(anticipation);
                redisTemplate.opsForZSet().add(now,ypname,score+1);
            }
        }
        return JSON.toJSONString("已期待");
    }

    @Override
    public String WantSee(HashMap json, HttpServletRequest request) {
        return null;
    }

    @Override
    public String  xianAnticipation(HashMap json, HttpServletRequest request) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(new Date());
        Date now = df.parse(date);
        TreeMap<String,Double> map = new TreeMap<>();
        for (int i=0;i<30;i++){
            Date timeSet = null;
            logger.info(String.format("now : %s",now));
            Calendar cal = Calendar.getInstance();
            cal.setTime(now);
            cal.add(Calendar.DATE,-i);
            timeSet = cal.getTime();
            logger.info(String.format("timeSet : %s",timeSet));
            String zsetName = df.format(timeSet);
            List<String> list =  new ArrayList<>(redisTemplate.opsForZSet().range(zsetName,0,-1));
            System.out.println("list:"+list.size());
            if (list.size()==0 || list==null){
                list = leaderboardDao.selectAnticipat(zsetName);
            }
            for (int j=0;j<list.size();j++){
                double zsetScore = redisTemplate.opsForZSet().score(zsetName,list.get(j));
                if (map.containsKey(list.get(j))){
                    double score = map.get(list.get(j));
                    map.put(list.get(j),score+zsetScore);
                }
                else {
                    map.put(list.get(j),zsetScore);
                }
            }
        }
        List<String> list = new ArrayList<>();
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry entry = (Map.Entry) iterator.next();
            list.add((String) entry.getKey());
        }
        List<Film> list1 = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            list1.add(leaderboardDao.selectFilmByypname(list.get(i)));
        }
        String jsons = JSON.toJSONString(list1);
        return jsons;

    }

    @Override
    public String allOrderListRedis(HashMap json, HttpServletRequest request) {
        List<String> list = new ArrayList<>(redisTemplate.opsForZSet().range(allOrderListRedis,0,100));
        if (list.size() == 0 ||list == null){
            list = leaderboardDao.selectAllOrderListRedis();
            List<Film> list1 = new ArrayList<>();
            for (int i=0;i<list.size();i++){
                list1.add(leaderboardDao.selectFilmByypname(list.get(i)));
            }
            return JSON.toJSONString(list1);
        }
        List<Film> list1 = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            list1.add(leaderboardDao.selectFilmByypname(list.get(i)));
        }
        for (int i=0;i<list1.size();i++){
            System.out.println(list1.get(i).getCountries());
        }
        String jsons = JSON.toJSONString(list1);
        return jsons;
    }


}
